package com.zhangzhan.blog_service.service.impl;

import com.zhangzhan.base.common_model.dtos.BlogLoginDto;
import com.zhangzhan.base.common_model.pojo.LoginUser;
import com.zhangzhan.base.common_model.result.ResponseResult;
import com.zhangzhan.base.common_model.vos.UserInfoVo;
import com.zhangzhan.base.enums.AppHttpCodeEnum;
import com.zhangzhan.base.utils.CopyBean;
import com.zhangzhan.base.utils.JwtUtil;
import com.zhangzhan.base.utils.RedisCache;
import com.zhangzhan.blog_model.vos.BlogUserLoginVo;
import com.zhangzhan.blog_service.service.BlogLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.zhangzhan.base.constant.RedisKey.LOGIN_KEY;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/2/2 22:27
 */
@Service
public class BlogLoginServiceImpl implements BlogLoginService {

    //前台传入参数调用security框架拦截器1 需要重写次接口才能注入
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;

    /**
     * @param dto
     * @return com.zhangzhan.base.common_model.result.ResponseResult
     * @description 博客前台登陆
     * @author zhangyiheng
     * @date 2024/2/2 22:28
     */
    @Override
    public ResponseResult login(BlogLoginDto dto) {
        //将用户名密码传入authenticationManager
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(dto.getUsername(), dto.getPassword());
        //次步骤会去调用security框架的userds的拦截器去尽显用户认证然后回来
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //判断是否存在
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("系统错误！请联系后端管理员。");
        }
        //校验完获取用户信息
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();

        //生产jwt请求体存入redis
        String jwt = JwtUtil.createJWT(loginUser.getUser().getId().toString());
        //存入redis
        redisCache.setCacheObject(LOGIN_KEY + loginUser.getUser().getId(), loginUser);
        //返回封装信息
        return ResponseResult.okResult(new BlogUserLoginVo(jwt, CopyBean.copyBean(loginUser.getUser(), UserInfoVo.class)));
    }

    /**
     * @return com.zhangzhan.base.common_model.result.ResponseResult
     * @description 博客前台退出登陆
     * @author zhangyiheng
     * @date 2024/2/4 22:28
     */
    @Override
    public ResponseResult logout() {
        //获取用户id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        Long id = loginUser.getUser().getId();
        //删除redis用户信息
        redisCache.deleteObject(LOGIN_KEY + id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"退出成功！");
    }
}
